# -*- coding: utf-8 -*-

import time
import unittest

from trade.IBExchange import Exchange
from data.model.Params import *
from data.model.Trade import *
from broker.tws.constant.Constant import *

import logging
import logs.syslog
log = logging.getLogger("syslog")

class IBExchangeTest(unittest.TestCase):
    accountId = "DU2082325"
    port = 5000
    exchange = Exchange(accountId, port)

    def test_tickle(self):
        ret, tickle = self.exchange.tickle()
        log.info("心跳检测： %s\t%s" % (ret, tickle))

    def test_GetTrades(self):
        ret, trades = self.exchange.GetTrades()
        tradeStrs = [str(x) for x in trades]
        log.info("交易记录： %s\t%s" % (ret, "\n".join(tradeStrs)))


    def test_GetAccountSummary(self):
        ret, summary = self.exchange.GetAccountSummary()
        log.info("Account Summary： %s\t%s" % (ret, summary))

    def test_GetAccountLedger(self):
        ret, ledger = self.exchange.GetAccountLedger()
        log.info("Account Ledger： %s\t%s" % (ret, ledger))

    def test_PreviewOrders(self):
        symbol = "US.TSLA"
        price = 1000.00
        side = Side.BUY
        quantity = 100
        orderReq = OrderReq.stock(symbol, price, side, quantity)
        ret, preorder = self.exchange.PreviewOrders(orderReq)
        log.info("最大可买可卖： %s\t%s" % (ret, preorder))

    def test_get_margin_ratio(self):
        code_list = ['HK.00700', 'HK.09988']
        Trade.get_margin_ratio(code_list)
